package dataStructure.queue;

import java.util.Scanner;

//数组实现队列
//循环队列

/**循环队列经过循环之后，新放入队头的数据不是头数据，头数据一定是front指向的数据！！！
 *
 */
public class ArrayToQueue {
    public static void main(String[] args) {
        Queue queue = new Queue(4);//说明设置 4, 其队列的有效数据最大是 3
        char key = ' '; // 接收用户输入
        Scanner scanner = new Scanner(System.in);//
        boolean loop = true;
// 输出一个菜单
        while (loop) {

            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);// 接收一个字符
            switch (key) {
                case 's':
                    queue.print();
                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.add(value);
                    break;

                case 'g': // 取出数据
                    try {
                        int res = queue.delete();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;

                case 'h': // 查看队列头的数据
                    try {
                        int res = queue.getHead();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
                        // TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;

                case 'e': // 退出
                    scanner.close();
                    loop = false;
                    break;
                default:

                    break;
            }
        }
        System.out.println("程序退出~~");


    }
}

class Queue {
    private int sizeMax;
    private int rear;   //队尾指针
    private int front;  //队头指针
    private int[] element;

    //构造函数
    public Queue(int sizeMax) {
        this.sizeMax = sizeMax;
        rear = 0;   //默认就是0
        front = 0;
        element = new int[sizeMax];
    }

    //判空
    private boolean isnull() {

        return rear == front;
    }

    //判满
    private boolean isfull() {
        return (rear + 1) % this.sizeMax == front;
    }

    public void add(int x) {

        if (!isfull()) {

            element[rear] = x;
            //考虑取模, 不考虑的话就是 rear++,考虑之后就是 (rear++)%sizeMax
            rear = (rear + 1) % sizeMax;
        } else {
            System.out.println("队满");
        }
    }

    //删除-----去掉队头元素
    public int delete() {
        if (!isnull()) {
           int value = this.element[front];
           front = (front+1)%sizeMax;
           return value;
        } else {
            throw new RuntimeException("队列为空");
        }
    }

    //输出  TODO 这需要想想
    public void print() {
        for (int i = front; i < front+size(); i++) {
            System.out.printf("element[%d]=%d\n",i%sizeMax,element[i%sizeMax]);
        }
    }

    //取出队头元素
    public int getHead() {
    if (isnull()){
        throw new RuntimeException("队列空的，没有数据~~");
    }
    return element[front];

    }

    //查看有多少数据
    public int size(){
        return (rear+sizeMax-front)%sizeMax;
    }
}
